<template>
  <div>
    <!-- 查询表单 -->
    <div class="check-form-box">
      <el-form
        :inline="true"
        :model="searchModel.data"
        class="demo-form-inline"
      >
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchModel.createDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="searchModel.state" placeholder="请选择状态">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="激活状态">
          <el-select v-model="searchModel.active" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="未激活" value="0" />
            <el-option label="已激活" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="车主ID">
          <el-input
            v-model.trim="searchModel.userId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入车主ID"
          />
        </el-form-item>
        <el-form-item label="一级代理商ID">
          <el-input
            v-model.trim="searchModel.agentFirstMasterId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入一级代理商ID"
          />
        </el-form-item>
        <el-form-item label="一级代理商名称">
          <el-input
            v-model.trim="searchModel.agentFirstMasterName"
            placeholder="请输入一级代理商名称"
          />
        </el-form-item>
        <el-form-item label="展业员ID">
          <el-input
            v-model.trim="searchModel.agentId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入展业员ID"
          />
        </el-form-item>
        <el-form-item label="展业员名称">
          <el-input
            v-model.trim="searchModel.agentName"
            placeholder="请输入展业员名称"
          />
        </el-form-item>
        <el-form-item label="对比结果">
          <el-select v-model="searchModel.compare" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="未上报" value="0" />
            <el-option label="解析中" value="1" />
            <el-option label="一致" value="2" />
            <el-option label="不一致" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="下单设备">
          <el-select v-model="searchModel.appPlatform" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option value="wx" label="微信小程序" />
            <el-option value="alipay" label="支付宝小程序" />
            <el-option value="android" label="android客户端" />
            <el-option value="ios" label="IOS 客户端" />
          </el-select>
        </el-form-item>
        <el-form-item label="下单来源">
          <el-select v-model="searchModel.bizPlatform" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option value="ybt" label="壹佰通" />
            <el-option value="cxw" label="车讯网" />
            <el-option value="wnsj" label="华行通" />
            <el-option value="kyx" label="阔悦行" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" color="#ea5b24" @click="searchOrder()"
            >查询</el-button
          >
          <el-button
            type="primary"
            color="#ea5b24"
            @click="exportOrder()"
            v-if="pinia.isShow[66]"
            >导出Excel</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 列表 -->
    <div>
      <!-- <el-table :data="tableData.data" style="width: 100%;margin-top: 20px;" :row-class-name="tableRowClassName"> -->
      <el-table :data="tableData.data" style="width: 100%; margin-top: 20px">
        <el-table-column prop="createTime" width="160" label="订单日期" />
        <el-table-column prop="carNo" width="100" label="车牌号" />
        <el-table-column
          prop="state"
          :formatter="judgeCompositeState"
          label="订单状态"
        />
        <el-table-column prop="active" label="激活状态">
          <template #default="scope">
            {{
              scope.row.active == null
                ? ""
                : scope.row.active == 0
                ? "未激活"
                : "已激活"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="appPlatform" width="120" label="下单设备">
          <template #default="scope">
            {{ judgePlatform(scope.row.appPlatform) }}
          </template>
        </el-table-column>
        <el-table-column prop="bizPlatform" label="下单来源">
          <template #default="scope">
            {{ judgePlatform(scope.row.bizPlatform) }}
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="车主ID" />
        <el-table-column prop="userCity" label="车主城市" />
        <el-table-column prop="agentFirstMasterId" label="展业员一级ID" />
        <el-table-column prop="agentFirstMasterName" label="展业员一级名称" />
        <el-table-column prop="agentId" label="展业员ID" />
        <el-table-column prop="agentName" label="展业员姓名" />
        <el-table-column prop="agentCity" label="展业员城市" />
        <el-table-column label="报备坐标" width="170">
          <template #default="scope">
            {{
              judgeLongitude(scope.row.auditLongitude, scope.row.auditLatitude)
            }}
          </template>
        </el-table-column>
        <el-table-column label="下单坐标" width="170">
          <template #default="scope">
            {{
              judgeLongitude(scope.row.orderLongitude, scope.row.orderLatitude)
            }}
          </template>
        </el-table-column>
        <el-table-column prop="compare" label="对比结果">
          <template #default="scope">
            <div :style="{ color: judgeCompareColor(scope.row) }">
              {{ judgeCompare(scope.row) }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页插件 -->
      <div style="text-align: center">
        <el-pagination
          :total="totalNum"
          @current-change="handleCurrentChange"
          background
          :page-size="size"
          layout="prev, pager, next, total"
          :current-page="page"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  findJobMonitoringApi,
  findJobMonitoringExcelApi,
} from "../../../api/index";
import { formatDateTime } from "../../../utils/formatDateTime";
import { usePermissStore } from "../../../store/permiss";
const pinia = usePermissStore();

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;
// 翻页
let flipData = reactive({
  page: page,
  size: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.page = page.value;
  findOrder(flipData);
};
// 搜索模型
const searchModel: any = reactive({
  state: null,
  active: null,
  userId: null,
  agentFirstMasterId: null,
  agentFirstMasterName: null,
  agentId: null,
  agentName: null,
  compare: null,
  appPlatform: null,
  bizPlatform: null,
  createDate: null,
  startTime: null,
  endTime: null,
  page: page,
  size: size,
});

// 状态枚举
const stateOptions: any = reactive([
  { value: null, label: "全部" },
  { value: "1", label: "未支付" },
  { value: "7", label: "支付中" },
  { value: "2", label: "已支付" },
  { value: "8", label: "支付失败" },
  { value: "4", label: "退款中" },
  { value: "5", label: "退款成功" },
  { value: "6", label: "退款失败" },
  { value: "9", label: "已取消" },
]);

// 判断状态显示
const judgeCompositeState = (row) => {
  switch (row.compositeState) {
    case 1:
      return "未支付";
    case 2:
      return "已支付";
    case 4:
      return "退款中";
    case 5:
      return "退款成功";
    case 6:
      return "退款失败";
    case 7:
      return "支付中";
    case 8:
      return "支付失败";
    case 9:
      return "已取消";
    default:
      return "";
  }
};

// 判断对比结果显示
const judgeCompare = (row) => {
  switch (row.compare) {
    case 0:
      return "未上报";
    case 1:
      return "解析中";
    case 2:
      return "一致";
    case 3:
      return "不一致";
    default:
      return "";
  }
};

// 设置单行样式
const tableRowClassName = (row) => {
  switch (row.row.compare) {
    case 0:
      return "notReportedColor";
    case 3:
      return "inconsistentColor";
    default:
      return "";
  }
};

// 设置对比结果颜色
const judgeCompareColor = (row) => {
  switch (row.compare) {
    case 0:
      return "#FFB931";
    case 3:
      return "#FF3D31";
    default:
      return "";
  }
};

// 判断设备来源文字显示
const judgePlatform = (str) => {
  switch (str) {
    case "wx":
      return "微信小程序";
    case "alipay":
      return "支付宝小程序";
    case "android":
      return "android客户端";
    case "ios":
      return "IOS 客户端";
    case "ybt":
      return "壹佰通";
    case "cxw":
      return "车讯网";
    case "wnsj":
      return "华行通";
    case "kyx":
      return "阔悦行";
    default:
      return "";
  }
};

// 过滤经纬度
const judgeLongitude = (str1, str2) => {
  if (str1 && str2) {
    return str1 + "," + str2;
  } else {
    return "";
  }
};

// 搜索业务订单
const searchOrder = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startTime = formatDateTime(searchModel.createDate[0]);
    searchModel.endTime = formatDateTime(searchModel.createDate[1]).replace(
      "00:00:00",
      "23:59:59"
    );
  } else {
    searchModel.startTime = null;
    searchModel.endTime = null;
  }
  searchParmSearch();
  findOrder(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};

// 查询业务订单
const findOrder = (searchModel) => {
  findJobMonitoringApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.data;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

//导出订单
const exportOrder = () => {
  findJobMonitoringExcelApi(searchModel).then((res) => {
    const blob = new Blob([res.data]);
    const fileName = "作业监控数据.xls";
    const elink = document.createElement("a"); // 创建a标签
    elink.download = fileName; // 为a标签添加download属性
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click(); // 点击下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink); // 释放标签
  });
};

onMounted(() => {
  findOrder(searchModel);
});
</script>

<style scoped>
:deep(.demo-form-inline) {
  display: grid;
  justify-items: stretch;
  grid-template-columns: repeat(auto-fill, 25%);
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-form-item) {
  width: 90%;
}

.item__txt {
  box-sizing: border-box;
  line-height: 24px;
  padding: 0 9px;
}

:deep(.notReportedColor) {
  color: #ffb931;
}

:deep(.inconsistentColor) {
  color: #ff3d31;
}
</style>
